package com.clip.service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clip.dao.UserDateDAO;
import com.clip.entity.User;
import com.clip.entity.UserDate;

@Service
@Transactional
public class UserDateService {
	@Autowired
	private UserDateDAO userDateDAO;
	
	public List<UserDate> getHoliday(User user,Integer year){
		String hql="FROM UserDate userDate where userDate.uid="+user.getId()+" and userDate.year="+year;
		List<UserDate> userDateList = userDateDAO.findByQuery(hql);
		return userDateList;
	}

	public Map<String,Object> orgHolidayList(List<UserDate> list){
		Map<String,Object> infoMap=new LinkedHashMap<String,Object>();
		for(UserDate userDate : list){
			Map<String,Object> userDateMap=new LinkedHashMap<String,Object>();
			userDateMap.put("year", userDate.getYear());
			userDateMap.put("month", userDate.getMonth());
			userDateMap.put("day", userDate.getDay());
			userDateMap.put("lastday", userDate.getLastdays());
			userDateMap.put("description", userDate.getName());
			infoMap.put("userdate id="+userDate.getId(), userDateMap);
		}
		return infoMap;
	}
	
	
	public List<UserDate> getUserHolidayByDay(User user,int year,int month,int day){
		String hql="FROM UserDate userDate where userDate.uid="+user.getId()+" and userDate.year="+year+" and userDate.month="+month+" and userDate.day="+day;
		List<UserDate> userDateList = userDateDAO.findByQuery(hql);
		return userDateList;
	}
	/**
	 * @param user	登录用户
	 * @param year	当前操作的年	
	 * @param holiday	节假日
	 */
	public void saveHoliday(User user,Integer year,String[] holiday){
		String hql="DELETE FROM UserDate userDate WHERE userDate.uid="+user.getId()+" and userDate.year="+year;
		userDateDAO.deleteByQuery(hql);
		for(String str : holiday){
			String[] nowDate = str.split("-");
			int yearDate = Integer.valueOf(nowDate[0]);
			int monthDate = Integer.valueOf(nowDate[1]);
			int dayDate = Integer.valueOf(nowDate[2]);
			String dayDesc = nowDate[3];
			int lastDate = Integer.valueOf(nowDate[4]);
			if(yearDate==year){
				UserDate userDate = new UserDate();
				userDate.setUid(user.getId());
				userDate.setYear(year);
				userDate.setMonth(monthDate);
				userDate.setDay(dayDate);
				userDate.setLastdays(lastDate);
				userDate.setName(dayDesc);
				userDateDAO.save(userDate);
			}
		}
	}
}
